package com.zb.service.imp.charger;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zb.dao.ext.dic.OmsDicForwardSrvInfoDao;
import com.zb.entity.base.JsonCommon;
import com.zb.entity.base.JsonForList;
import com.zb.entity.base.OmsCommonParam;
import com.zb.entity.dic.OmsDicForwardSrvInfo;
import com.zb.entity.exception.OmsDaoException;
import com.zb.service.charger.DicForwardSrvInfoService;
import com.zb.service.imp.base.BaseService;
import com.zb.util.OmsConstant.MsgCodeEnum;
@Service @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class DicForwardSrvInfoServiceImpl extends BaseService implements DicForwardSrvInfoService
{
	@Autowired
	private OmsDicForwardSrvInfoDao dicForwardSrvInfoDaoImpl;
	@Override
	public synchronized JsonCommon<?> add(OmsDicForwardSrvInfo dicForwardSrvInfo) 
	{
		try 
		{
			String maxId = this.genarateId(dicForwardSrvInfoDaoImpl.findMaxId(OmsDicForwardSrvInfo.class), 10);
			if (StringUtils.isNotEmpty(maxId))
			{
				dicForwardSrvInfoDaoImpl.add(dicForwardSrvInfo);
				return this.buildSuccessJsonCommon(dicForwardSrvInfo);
			}
		}
		catch (OmsDaoException e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
		return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:genarate id fail");
	}

	@Override
	public JsonCommon<?> update(OmsDicForwardSrvInfo dicForwardSrvInfo) 
	{
		try 
		{
			if (dicForwardSrvInfo.getForward_srv_id()==null)
			{
				return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request id is empty");
			}
			dicForwardSrvInfoDaoImpl.update(dicForwardSrvInfo);
			return this.buildSuccessJsonCommon(dicForwardSrvInfo);
		}
		catch (OmsDaoException e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonCommon<?> deleteLogic(String forward_srv_id) 
	{
		if (StringUtils.isEmpty(forward_srv_id))
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request id is empty");
		}
		try 
		{
			dicForwardSrvInfoDaoImpl.deleteById(forward_srv_id, OmsDicForwardSrvInfo.class);
			return this.buildSuccessJsonCommon("success");
		} catch (OmsDaoException e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

	@Override
	public JsonForList<?> findPageObjs(OmsCommonParam param, OmsDicForwardSrvInfo obj) {
		try 
		{
			List<OmsDicForwardSrvInfo> lst = dicForwardSrvInfoDaoImpl.pagingFind(param, obj);
			int total = 0;
			if (param.isQueryTotal())
			{
				total = dicForwardSrvInfoDaoImpl.queryTotalByObject(obj);
			}
			return this.buildSuccessJsonForList(param, lst, total);
		} catch (Exception e) {
			this.logError(e.getMessage(), e);
			return this.buildError400JsonForList();
		}
	}

	@Override
	public JsonCommon<?> findObjByPk(String forward_srv_id) {
		if (StringUtils.isEmpty(forward_srv_id))
		{
			return this.buildOtherJsonCommon(MsgCodeEnum.ERROR400.getMsgCode(), "Error:the request id is empty");
		}
		try 
		{
			OmsDicForwardSrvInfo obj = new OmsDicForwardSrvInfo();
			obj.setForward_srv_id(Integer.valueOf(forward_srv_id));
			obj = dicForwardSrvInfoDaoImpl.findRecordByPK(obj);
			return this.buildSuccessJsonCommon(obj);
		} 
		catch (Exception e) 
		{
			this.logError(e.getMessage(), e);
			return this.buildError400JsonCommon();
		}
	}

}
